﻿using System.IO;
using System.Net;
using System.Windows;
using Bookvar.Common;
using Bookvar.ObjectModel;
using Bookvar.ObjectModel.Actions;
using Bookvar.UI.Actions;
using Bookvar.UI.ViewModel.LiveImages;

namespace Bookvar.UI.DragDrop.Actions
{
    public class ImageDropAction : TopicDropActionBase
    {
        private Resource imageResource;
        private TopicContentPresenter topicContentPresenter;
        private CompositeAction actions;

        public override void Execute( CompositeAction compositeAction, TopicContentPresenter presenter, Point dropPoint )
        {
            topicContentPresenter = presenter;
            actions = compositeAction;

            if ( Data.GetDataPresent( DataFormats.FileDrop ) )
            {
                HandleFileDrop();
            }

            if ( Data.GetDataPresent( BookvarDataFormats.ImageInfo.Name ) )
            {
                HandleImageInfo();
            }

            if ( Data.GetDataPresent( BookvarDataFormats.LiveImageInfo.Name ) )
            {
                HandleLiveImageInfo();
            }
        }

        private void HandleLiveImageInfo()
        {
            var imageInfo = (LiveImageInfo) Data.GetData( BookvarDataFormats.LiveImageInfo.Name );
            var action = new AddWebImageAction( imageInfo.ImageInfo, imageInfo.ImageSize, imageInfo.ThumbnailSource );
            action.TargetTopic = topicContentPresenter.Topic;

            actions.Add( action );
            ActionService.Execute( actions );
        }

        private void HandleFileDrop()
        {
            var fileNames = (string[]) Data.GetData( DataFormats.FileDrop );
            if ( fileNames != null && fileNames.Length > 0 )
            {
                imageResource = new Resource();
                imageResource.ResourcePath = Path.GetFileName( fileNames[ 0 ] );
                using ( var resourceStream = new FileStream( fileNames[ 0 ], FileMode.Open, FileAccess.Read ) )
                {
                    AddImageResource( resourceStream );
                }
            }
        }

        private void HandleImageInfo()
        {
            var info = (ImageInfo) Data.GetData( BookvarDataFormats.ImageInfo.Name );

            if ( info != null )
            {
                imageResource = new Resource();
                imageResource.ResourceType = ResourceType.Image;
                imageResource.ResourcePath = info.Title;
                var client = new WebClient();
                try
                {
                    using ( var resourceStream = new MemoryStream( client.DownloadData( info.ImageUri ) ) )
                    {
                        AddImageResource( resourceStream );
                    }
                }
                catch ( WebException ex )
                {
                    Logger.LogProvider.Log( ex.ToString() );
                    imageResource = null;
                }
                finally
                {
                    client.Dispose();
                }
            }
        }

        private void AddImageResource( Stream resourceStream )
        {
            if ( imageResource != null )
            {
                string fileName = Path.GetTempFileName();
                using ( var fileSteam = new FileStream( fileName, FileMode.OpenOrCreate, FileAccess.Write ) )
                {
                    StreamUtility.CopyStream( resourceStream, fileSteam );
                }

                var addImageAction =
                    TopicActions.CreateAddResourceAction( imageResource, fileName, topicContentPresenter.Topic,
                                                          topicContentPresenter.Owner.Mindmap );

                actions.Add( addImageAction );
                ActionService.Execute( actions );
            }
        }
    }
}